let UserDataManager = require("UserDataManager");
let GameDataManager = require("GameDataManager");
let WX = require("WX");
let MiniP = require("MiniP");


let Menu = cc.Class({
    extends: cc.Component,

    properties: {
        carBtn: cc.Node,
        playBtn: cc.Node,
        dollarLabel: cc.Label,

    },

    statics: {
        instance: null,
    },

    ctor() {
        if (!Menu.instance) {
            Menu.instance = this;
        }
    },

    onEnable() {
        this.updateDollarLabel();
        this.updateCarBtn();
    },

    onLoad() {
        this.playBtn.runAction(
            cc.repeatForever(
                cc.sequence(
                    cc.scaleTo(0.3, 1.1),
                    cc.scaleTo(0.3, 1)
                )
            )
        );
    },

    updateDollarLabel() {
        // this.dollarLabel.string = "￥" + UserDataManager.getUserData().dollar;
        this.dollarLabel.string = UserDataManager.getUserData().dollar;
    },

    updateCarBtn() {
        let Car = this.carBtn.getChildByName("Car");
        let carLevel = UserDataManager.getUserData().carLevel;
        for (let car of Car.children) car.active = false;
        Car.getChildByName("car_" + carLevel).active = true;

        let carPrice = GameDataManager.getCarPrice(carLevel + 1);
        if (carPrice <= UserDataManager.getUserData().dollar) {
            this.carBtn.runAction(cc.repeatForever(
                cc.sequence(
                    cc.scaleTo(0.5, 1.05),
                    cc.scaleTo(0.5,1)
                )
            ));
            this.carBtn.getChildByName("new_car").active = true;
        } else {
            this.carBtn.getChildByName("new_car").active = false;
            this.carBtn.stopAllActions();
        }
    },



    start() {

    },

    open() {
        console.log("Menu-open");
        WX.showBannerAd()
        this.node.active = true;
        MiniP.instance.init();
    },

    hide() {
        console.log("Menu-hide");
        WX.hideBannerAd()
        this.node.active = false;
    }

    // update (dt) {},
});
